package com.zhaofan.other;

import com.sun.org.apache.bcel.internal.generic.NEW;

import javax.lang.model.element.VariableElement;
import java.util.HashMap;
import java.util.Map;

/**
 * 12. 整数转罗马数字
 * https://leetcode-cn.com/problems/integer-to-roman/
 */
public class No12 {
    public static void main(String[] args) {
        System.out.println(intToRoman2(1994));
    }

    /**
     * 数组中已经将所有的特殊结果枚举出,没有出现的结果都可以通过简单的拼接字符串来实现
     * 所以循环数组,当前值大于values[i]时表示要拼接对应的字符串;
     * @param num
     * @return
     */
    public static String intToRoman(int num) {
        int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        StringBuilder sb = new StringBuilder();
        int n=num;
        for (int i=0;i<values.length;){
            if(n<=0) break;
            if(n>=values[i]){
                n-=values[i];
                sb.append(symbols[i]);
            }else{
                ++i;
            }
        }
        return sb.toString();
    }

    public static String intToRoman2(int num) {
        String[] thousand={"","M","MM","MMM"};
        String[] hundred={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
        String[] ten={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
        String[] g={"","I","II","III","IV","V","VI","VII","VIII","IX"};
        StringBuilder sb = new StringBuilder();
        String s=thousand[num/1000]+hundred[num%1000/100]+ten[num%100/10]+g[num%10];
        return s;
    }
}
